package com.horoscope.astrology.zodiac.signs.free.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.horoscope.astrology.zodiac.signs.free.R;
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils;

public class ProfileAdapter extends RecyclerView.Adapter {

    private String[] headName;
    private String[] headVal;
    private OnItemClickedListener mOnItemClickedListener = null;

    public ProfileAdapter(String[] headName, String[] headVal) {
        this.headName = headName;
        this.headVal = headVal;
    }

    public void update(int index, String val) {
        headVal[index] = val;
        notifyDataSetChanged();
    }

    public interface OnItemClickedListener {
        void OnItemClicked(View view, int position);
    }

    public void setOnClickedListener(OnItemClickedListener listener) {
        mOnItemClickedListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(
                viewGroup.getContext()).inflate(R.layout.fragment_recy_profile,
                viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        final int position = i;
        ViewHolder vh = (ViewHolder) viewHolder;
        vh.headName.setText(headName[i]);
        vh.headVal.setText(headVal[i]);
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!SysUtils.isFastClick()) {
                    if (mOnItemClickedListener != null) {
                        mOnItemClickedListener.OnItemClicked(v, position);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return headName == null ? 0 : headName.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView headName;
        TextView headVal;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            headName = itemView.findViewById(R.id.recy_profile_headName);
            headVal = itemView.findViewById(R.id.recy_profile_headVal);
        }
    }
}
